﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using FeedAntGame.Control;
using Microsoft.Xna.Framework;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;
using FeedAntGame.Support;

namespace FeedAntGame.Component.Dialog
{
    class MenuDlg : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D bg;
        SpriteBatch spriteBatch;
        Button resumeButton, mainMenuButton;
        Checkbox fullScreen, sound;
        Vector2 bgPos;

        List<BaseControl> controlList = new List<BaseControl>();
        public MenuDlg(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
          
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            bg = Game.Content.Load<Texture2D>(@"Images\menu_background");
            bgPos = new Vector2(Game.Window.ClientBounds.Width / 2 - bg.Width / 2,
                Game.Window.ClientBounds.Height / 2 - bg.Height / 2);
            //resume button
            resumeButton = new Button(Game, Button.TYPE.RESUME2, bgPos + new Vector2(80, 220));
            EventSprite resumeSprite = (EventSprite)resumeButton.Sprite;
            resumeSprite.Click += new EventSprite.ClickButton(onResumeClick);
            resumeSprite.Hover += new EventSprite.HoverButton(onShowHand);
            resumeSprite.Scale = 2;
            controlList.Add(resumeButton);
            //main menu button
            mainMenuButton = new Button(Game, Button.TYPE.MAIN_MENU, bgPos + new Vector2(100, 180));
            EventSprite mainMenuSprite = (EventSprite)mainMenuButton.Sprite;
            mainMenuSprite.Click += new EventSprite.ClickButton(onMainMenuClick);
            mainMenuSprite.Hover += new EventSprite.HoverButton(onShowHand);
            mainMenuSprite.Scale = 1.25f;            
            controlList.Add(mainMenuButton);
            // sound check box
            sound = new Checkbox(Game, Checkbox.TYPE.SOUND, new Vector2(bgPos.X + bg.Width - 30, bgPos.Y + 30));
            sound.Sprite.Position = new Vector2(bgPos.X + bg.Width - sound.Sprite.Width - 50, bgPos.Y - sound.Sprite.Height / 2 + 80);
            sound.Sprite.Scale = 0.5f;
            (sound.Sprite as EventSprite).Click += new EventSprite.ClickButton(onSoundClick);
            (sound.Sprite as CheckboxSprite).Toogle = true; // set sound on
            (sound.Sprite as EventSprite).Hover += new EventSprite.HoverButton(onShowHand);
            controlList.Add(sound);
            //full screen check box
            fullScreen = new Checkbox(Game, Checkbox.TYPE.SOUND, new Vector2(bgPos.X + bg.Width - 30, bgPos.Y + 30));
            fullScreen.Sprite.Position = new Vector2(bgPos.X + bg.Width - fullScreen.Sprite.Width - 50, bgPos.Y - sound.Sprite.Height / 2 + 105);
            fullScreen.Sprite.Scale = 0.5f;
            (fullScreen.Sprite as EventSprite).Click += new EventSprite.ClickButton(onFullScreenClick);
            (fullScreen.Sprite as CheckboxSprite).Toogle = false; // default normal, not full screen
            (fullScreen.Sprite as EventSprite).Hover += new EventSprite.HoverButton(onShowHand);
            controlList.Add(fullScreen);
            base.LoadContent();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            for (int i = 0; i < controlList.Count; i++)
            {
                BaseControl control = controlList.ElementAt(i);
                control.Sprite.Update(gameTime, Game.Window.ClientBounds);
                if ((control.Sprite as EventSprite).IsPlaySoundHover())
                {
                    ((GameFA)Game).PlayCue(control.Sprite.collisionCueName);
                }
            }
                
            

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Rectangle((int)bgPos.X, (int)bgPos.Y, bg.Width, bg.Height), Color.White);
            foreach (BaseControl c in controlList)
            {
                c.Sprite.Draw(gameTime, spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void onResumeClick()
        {
            Finish();
        }
        private void onMainMenuClick()
        {
            //save game
            PlayManager playManager = ((GameFA)Game).playManager;
            Database.Hien_Trang.LuuHienTrang(playManager.GetCurrentLevel(),(int) playManager.GetLastScore());
            //close this dlg
            //Finish();
            // go to main menu
            ((GameFA)Game).GoToMainMenu();
        }
        private void onFullScreenClick()
        {
            if ((fullScreen.Sprite as CheckboxSprite).Toogle == true)
            {
                // full screen
                ((GameFA)Game).setFullScreen(true);
            }
            else
            {
                // normal
                ((GameFA)Game).setFullScreen(false);
            }
        }
        private void onSoundClick()
        {
            if ((sound.Sprite as CheckboxSprite).Toogle == true)
            {
                // sound on
                ((GameFA)Game).Volume(1, "Music");
            }
            else
            {
                // sound off
                ((GameFA)Game).Volume(0, "Music");
            }
        }
        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }

        public void Start()
        {
            this.Enabled = true;
            this.Visible = true;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is MenuDlg) && c.Visible == true && c.Enabled == true)
                {
                    c.Enabled = false;
                }
            }
        }

        private void Finish()
        {
            
            this.Enabled = false;
            this.Visible = false;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is MenuDlg) && c.Visible == true && c.Enabled == false)
                {
                    c.Enabled = true;
                }
            }
        }
    }
}
